package com.wms.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wms.dto.ConfirmWeightsDto;
import com.wms.mapper.OutboundOrderDetailsMapper;
import com.wms.mapper.OutboundOrderMapper;
import com.wms.mapper.OutboundOrderWeightsMapper;
import com.wms.pojo.OutboundOrder;
import com.wms.pojo.OutboundOrderDetails;
import com.wms.pojo.OutboundOrderWeights;
import com.wms.pojo.PageResult;
import com.wms.service.OutboundOrderDetailsService;
import com.wms.service.OutboundOrderWeightsService;
import com.wms.vo.OutboundOrderWeightsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class OutboundOrderWeightsServiceImpl extends ServiceImpl<OutboundOrderWeightsMapper, OutboundOrderWeights> implements OutboundOrderWeightsService {

    @Autowired
    private OutboundOrderDetailsMapper detailsMapper;
    @Autowired
    private OutboundOrderMapper orderMapper;

    @Override
    public PageResult queryPage(Integer page, Integer pageSize, Integer status, String weightsCode) {
        PageHelper.startPage(page,pageSize);
        List<OutboundOrderWeightsVo> list = getBaseMapper().queryPage(status,weightsCode);
        Page<OutboundOrderWeightsVo> weightsVoPage = (Page<OutboundOrderWeightsVo>) list;
        return new PageResult(weightsVoPage.getTotal(),weightsVoPage.getResult());
    }

    @Override
    @Transactional
    public void updateByIdStatus(ConfirmWeightsDto dto) {
        OutboundOrderWeights weights = getById(dto.getId());
        OutboundOrderWeights orderWeights = OutboundOrderWeights.builder()
                .id(dto.getId())
                .status(2)
                .weightVal(dto.getWeightVal())
                .weightsEmpId(dto.getWeightsEmpId())
                .weightsTime(dto.getWeightsTime())
                .weightsUnit(dto.getWeightsUnit())
                .updateTime(LocalDateTime.now())
                .build();
        updateById(orderWeights);

        //修改发货单的状态
        OutboundOrderDetails outboundOrderDetails = detailsMapper.selectById(weights.getOutboundOrderDetailId());
        OutboundOrder outboundOrder = orderMapper.selectById(outboundOrderDetails.getOutboundOrderId());
        OutboundOrder order = OutboundOrder.builder().id(outboundOrder.getId())
                .status(7)
                .updateTime(LocalDateTime.now())
                .build();
        orderMapper.updateById(order);
    }
}
    